package com.hnevc.acountms.activity;

import android.app.DatePickerDialog;
import android.icu.util.Calendar;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.hnevc.acountms.R;
import com.hnevc.acountms.model.Tb_inaccount;

public class AddInAccountActivity extends AppCompatActivity {
    private EditText mEtInMoney;
    private TextView mTvInTime;
    private Spinner mEtInType;
    private EditText mEtInHandler;
    private EditText mEtInMark;
    private Button mBtnAdd;
    private Button mBtnCancel;


    //In Time
    private int mYear;
    private int mMonth;
    private int mDay;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_in_account);
        initView();
        initEvent();
    }

    private void initEvent() {

        mTvInTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerDialog dialog = null;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    dialog = new DatePickerDialog(AddInAccountActivity.this);

                    dialog.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                            mYear = year;
                            mMonth = month;
                            mDay = dayOfMonth;
                            mTvInTime.setText(mYear + "-" + (mMonth+1) + "-" + mDay);
                        }
                    });
                    dialog.show();
                }
            }
        });
        mBtnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //添加收入记录
                Tb_inaccount inaccount = new Tb_inaccount();
                inaccount.setMoney(Double.parseDouble(mEtInMoney.getText().toString()));
                inaccount.setTime(mTvInTime.getText().toString());
                inaccount.setType(mEtInType.getSelectedItem().toString());
                inaccount.setHandler(mEtInHandler.getText().toString());
                inaccount.setMark(mEtInMark.getText().toString());
                boolean result = inaccount.save();
                if (result) {
                    Toast.makeText(AddInAccountActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(AddInAccountActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
                }


            }
        });
        mBtnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void initView() {
        mEtInMoney = (EditText) findViewById(R.id.id_et_money);
        mTvInTime = (TextView) findViewById(R.id.id_tv_time);
        initTime();
        mEtInType = (Spinner) findViewById(R.id.id_sp_type);
        mEtInHandler = (EditText) findViewById(R.id.id_et_inhandler);
        mEtInMark = (EditText) findViewById(R.id.id_et_inmark);
        mBtnAdd = (Button) findViewById(R.id.id_btn_add);
        mBtnCancel = (Button) findViewById(R.id.id_btn_cancel);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void initTime() {
        Calendar calendar = Calendar.getInstance();
        mYear = calendar.get(Calendar.YEAR);
        mMonth = calendar.get(Calendar.MONTH);
        mDay = calendar.get(Calendar.DAY_OF_MONTH);
        mTvInTime.setText(mYear + "-" + (mMonth+1)  + "-" + mDay);
    }


}
